home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 1 / LSD Compendium Deluxe 1.iso / a / text / manipulation / snap164.lha / minrexx.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-30  |  15.2 KB  |  451 lines

  1. #if __SASC
  2. #include "snap.h"
  3. #endif
  4.  
  5. /*
  6.  *   This is an example of how REXX messages might be handled.  This is
  7.  *   a `minimum' example that both accepts asynchronous REXX messages and
  8.  *   can request REXX service.
  9.  *
  10.  *   Read this entire file!  It's short enough.
  11.  *
  12.  *   It is written in such a fashion that it can be attached to a program
  13.  *   with a minimum of fuss.  The only external symbols it makes available
  14.  *   are the seven functions and RexxSysBase.
  15.  *
  16.  *   This code is by Radical Eye Software, but it is put in the public
  17.  *   domain.  I would appreciate it if the following string was left in
  18.  *   both as a version check and as thanks from you for the use of this
  19.  *   code.
  20.  *
  21.  *   If you modify this file for your own use, don't bump the version
  22.  *   number; add a suffix, such as 1.0a or 1.0.3 or something, so we
  23.  *   don't have fake `versions' floating around.
  24.  */
  25. static char *blurb = "Radical Eye MinRexx 0.4" ;
  26. /*
  27.  *   We read in our own personal little include.
  28.  */
  29. #include "minrexx.h"
  30. /*
  31.  *   All of our local globals, hidden from sight.
  32.  */
  33. static struct MsgPort *rexxPort ;          /* this is *our* rexx port */
  34. static int bringerdown ;                   /* are we trying to shut down? */
  35. static struct rexxCommandList *globalrcl ; /* our command association list */
  36. static long stillNeedReplies ;             /* how many replies are pending? */
  37. static long rexxPortBit ;                  /* what bit to wait on for Rexx? */
  38. static char *extension ;                   /* the extension for macros */
  39. static int (*userdisp)() ;                 /* the user's dispatch function */
  40. static struct RexxMsg *oRexxMsg ;          /* the outstanding Rexx message */
  41. /*
  42.  *   Our library base.  Don't you dare close this!
  43.  */
  44. struct Library *RexxSysBase ;
  45. /*
  46.  *   This is the main entry point into this code.
  47.  */
  48. long upRexxPort(s, rcl, exten, uf)
  49. /*
  50.  *   The first argument is the name of your port to be registered;
  51.  *   this will be used, for instance, with the `address' command of ARexx.
  52.  */
  53. char *s ;
  54. /*
  55.  *   The second argument is an association list of command-name/user-data
  56.  *   pairs.  It's an array of struct rexxCommandList, terminated by a
  57.  *   structure with a NULL in the name field. The commands are case
  58.  *   sensitive.  The user-data field can contain anything appropriate,
  59.  *   perhaps a function to call or some other data.
  60.  */
  61. struct rexxCommandList *rcl ;
  62. /*
  63.  *   The third argument is the file extension for ARexx macros invoked
  64.  *   by this program.  If you supply this argument, any `primitive' not
  65.  *   in the association list rcl will be sent out to ARexx for
  66.  *   interpretation, thus allowing macro programs to work just like
  67.  *   primitives.  If you do not want this behavior, supply a `NULL'
  68.  *   here, and those commands not understood will be replied with an
  69.  *   error value of RXERRORNOCMD.
  70.  */
  71. char *exten ;
  72. /*
  73.  *   The fourth argument is the user dispatch function.  This function
  74.  *   will *only* be called from rexxDisp(), either from the user calling
  75.  *   this function directly, or from dnRexxPort().  Anytime a command
  76.  *   match is found in the association list, this user-supplied function
  77.  *   will be called with two arguments---the Rexx message that was
  78.  *   received, and a pointer to the association pair.  This function
  79.  *   should return a `1' if the message was replied to by the function
  80.  *   and a `0' if the default success code of (0, 0) should be returned.
  81.  *   Note that the user function should never ReplyMsg() the message;
  82.  *   instead he should indicate the return values with replyRexxCmd();
  83.  *   otherwise we lose track of the messages that still lack replies.
  84.  */
  85. int (*uf)() ;
  86. /*
  87.  *   upRexxPort() returns the signal bit to wait on for Rexx messages.
  88.  *   If something goes wrong, it simply returns a `0'.  Note that this
  89.  *   function is safe to call multiple times because we check to make
  90.  *   sure we haven't opened already.  It's also a quick way to change
  91.  *   the association list or dispatch function.
  92.  */
  93. {
  94.    struct MsgPort *FindPort() ;
  95.    struct MsgPort *CreatePort() ;
  96.  
  97. /*
  98.  *   Some basic error checking.
  99.  */
  100.    if (rcl == NULL || uf == NULL)
  101.       return(0L) ;
  102. /*
  103.  *   If we aren't open, we make sure no one else has opened a port with
  104.  *   this name already.  If that works, and the createport succeeds, we
  105.  *   fill rexxPortBit with the value to return.
  106.  *
  107.  *   Note that rexxPortBit will be 0 iff rexxPort is NULL, so the check
  108.  *   for rexxPort == NULL also insures that our rexxPortBit is 0.
  109.  */
  110.    if (rexxPort == NULL) {
  111.       Forbid() ;
  112.       if (FindPort(s)==NULL)
  113.          rexxPort = CreatePort(s, 0L) ;
  114.       Permit() ;
  115.       if (rexxPort != NULL)
  116.          rexxPortBit = 1L << rexxPort->mp_SigBit ;
  117.    }
  118. /*
  119.  *   Squirrel away these values for our own internal access, and return
  120.  *   the wait bit.
  121.  */
  122.    globalrcl = rcl ;
  123.    extension = exten ;
  124.    userdisp = uf ;
  125.    return(rexxPortBit) ;
  126. }
  127. /*
  128.  *   This function closes the rexx library, but only if it is open
  129.  *   and we aren't expecting further replies from REXX.  It's
  130.  *   *private*, but it doesn't have to be; it's pretty safe to
  131.  *   call anytime.
  132.  */
  133. static void closeRexxLib() {
  134.    if (stillNeedReplies == 0 && RexxSysBase) {
  135.       CloseLibrary((struct Library *)RexxSysBase) ;
  136.       RexxSysBase = NULL ;
  137.    }
  138. }
  139. /*
  140.  *   This function closes down the Rexx port.  It is always safe to
  141.  *   call, and should *definitely* be made a part of your cleanup
  142.  *   routine.  No arguments and no return.  It removes the Rexx port,
  143.  *   replies to all of the messages and insures that we get replies
  144.  *   to all the ones we sent out, closes the Rexx library, deletes the
  145.  *   port, clears a few flags, and leaves.
  146.  */
  147. void dnRexxPort() {
  148.    if (rexxPort) {
  149.       RemPort(rexxPort) ;
  150.       bringerdown = 1 ;
  151. /*
  152.  *   A message still hanging around?  We kill it off.
  153.  */
  154.       if (oRexxMsg) {
  155.          oRexxMsg->rm_Result1 = RXERRORIMGONE ;
  156.          ReplyMsg((struct Message *)oRexxMsg) ;
  157.          oRexxMsg = NULL ;
  158.       }
  159.       while (stillNeedReplies) {
  160.          WaitPort(rexxPort) ;
  161.          dispRexxPort() ;
  162.       }
  163.       closeRexxLib() ;
  164.       DeletePort(rexxPort) ;
  165.       rexxPort = NULL ;
  166.    }
  167.    rexxPortBit = 0 ;
  168. }
  169. /*
  170.  *   Here we dispatch any REXX messages that might be outstanding.
  171.  *   This is the main routine for handling Rexx messages.
  172.  *   This function is fast if no messages are outstanding, so it's
  173.  *   pretty safe to call fairly often.
  174.  *
  175.  *   If we are bring the system down and flushing messages, we reply
  176.  *   with a pretty serious return code RXERRORIMGONE.
  177.  *
  178.  *   No arguments, no returns.
  179.  */
  180. static int cmdcmp(register char * c, register char * m) ;
  181.  
  182. void dispRexxPort() {
  183.    register struct RexxMsg *RexxMsg ;
  184.    register struct rexxCommandList *rcl ;
  185.    register char *p ;
  186.    register int dontreply ;
  187.  
  188. /*
  189.  *   If there's no rexx port, we're out of here.
  190.  */
  191.    if (rexxPort == NULL)
  192.       return ;
  193. /*
  194.  *   Otherwise we have our normal loop on messages.
  195.  */
  196.    while (RexxMsg = (struct RexxMsg *)GetMsg(rexxPort)) {
  197. /*
  198.  *   If we have a reply to a message we sent, we look at the second
  199.  *   argument.  If it's set, it's a function we are supposed to call
  200.  *   so we call it.  Then, we kill the argstring and the message
  201.  *   itself, decrement the outstanding count, and attempt to close
  202.  *   down the Rexx library.  Note that this call only succeeds if
  203.  *   there are no outstanding messages.  Also, it's pretty quick, so
  204.  *   don't talk to me about efficiency.
  205.  */
  206.       if (RexxMsg->rm_Node.mn_Node.ln_Type == NT_REPLYMSG) {
  207.          if (RexxMsg->rm_Args[1]) {
  208.             ((int (*)())(RexxMsg->rm_Args[1]))(RexxMsg) ;
  209.          }
  210.          DeleteArgstring(RexxMsg->rm_Args[0]) ;
  211.          DeleteRexxMsg(RexxMsg) ;
  212.          stillNeedReplies-- ;
  213.          closeRexxLib() ;
  214. /*
  215.  *   The default case is we got a message and we need to check it for
  216.  *   primitives.  We skip past any initial tabs or spaces and initialize
  217.  *   the return code fields.
  218.  */
  219.       } else {
  220.          p = (char *)RexxMsg->rm_Args[0] ;
  221.          while (*p > 0 && *p <= ' ')
  222.             p++ ;
  223.          RexxMsg->rm_Result1 = 0 ;
  224.          RexxMsg->rm_Result2 = 0 ;
  225. /*
  226.  *   If somehow the reply is already done or postponed, `dontreply' is
  227.  *   set.
  228.  */
  229.          dontreply = 0 ;
  230. /*
  231.  *   If the sky is falling, we just blow up and replymsg.
  232.  */
  233.          if (bringerdown) {
  234.             RexxMsg->rm_Result1 = RXERRORIMGONE ;
  235. /*
  236.  *   Otherwise we cdr down our association list, comparing commands,
  237.  *   until we get a match.  If we get a match, we call the dispatch
  238.  *   function with the appropriate arguments, and break out.
  239.  */
  240.          } else {
  241.             oRexxMsg = RexxMsg ;
  242.             for (rcl = globalrcl; rcl->name; rcl++) {
  243.                if (cmdcmp(rcl->name, p) == 0) {
  244.                   userdisp(RexxMsg, rcl, p+strlen(rcl->name)) ;
  245.                   break ;
  246.                }
  247.             }
  248. /*
  249.  *   If we broke out, rcl will point to the command we executed; if we
  250.  *   are at the end of the list, we didn't understand the command.  In
  251.  *   this case, if we were supplied an extension in upRexxPort, we know
  252.  *   that we should send the command out, so we do so, synchronously.
  253.  *   The synchronous send takes care of our reply.  If we were given a
  254.  *   NULL extension, we bitch that the command didn't make sense to us.
  255.  */
  256.             if (rcl->name == NULL) {
  257.                if (extension) {
  258.                   syncRexxCmd(RexxMsg->rm_Args[0], RexxMsg) ;
  259.                   dontreply = 1 ;
  260.                } else {
  261.                   RexxMsg->rm_Result1 = RXERRORNOCMD ;
  262.                }
  263.             }
  264.          }
  265. /*
  266.  *   Finally, reply if appropriate.
  267.  */
  268.          oRexxMsg = NULL ;
  269.          if (! dontreply)
  270.             ReplyMsg((struct Message *)RexxMsg) ;
  271.       }
  272.    }
  273. }
  274. /*
  275.  *   This is the function we use to see if the command matches
  276.  *   the command string.  Not case sensitive, and the real command only
  277.  *   need be a prefix of the command string.  Make sure all commands
  278.  *   are given in lower case!
  279.  */
  280. static int cmdcmp(c, m)
  281. register char *c, *m ;
  282. {
  283.    while (*c && ((*c == *m) || (*c == *m + 32 && ('a' <= *c && *c <= 'z')))) {
  284.       c++ ;
  285.       m++ ;
  286.    }
  287.    return((int)*c) ;
  288. }
  289. /*
  290.  *   Opens the Rexx library if unopened.  Returns success (1) or
  291.  *   failure (0).  This is another function that is *private* but
  292.  *   that doesn't have to be.
  293.  */
  294. static int openRexxLib() {
  295.    if (RexxSysBase)
  296.       return(1) ;
  297.    return((RexxSysBase = (struct RxsLib *)OpenLibrary(RXSNAME, 0L)) != NULL) ;
  298. }
  299. /*
  300.  *   This is the general ARexx command interface, but is not the one
  301.  *   you will use most of the time; ones defined later are easier to
  302.  *   understand and use.  But they all go through here.
  303.  */
  304. struct RexxMsg *sendRexxCmd(s, f, p1, p2, p3)
  305. char *s ;
  306. /*
  307.  *   The first parameter is the command to send to Rexx.
  308.  */
  309. int (*f)() ;
  310. /*
  311.  *   The second parameter is either NULL, indicating that the command
  312.  *   should execute asynchronously, or a function to be called when the
  313.  *   message we build up and send out here finally returns.  Please note
  314.  *   that the function supplied here could be called during cleanup after
  315.  *   a fatal error, so make sure it is `safe'.  This function always is
  316.  *   passed one argument, the RexxMsg that is being replied.
  317.  */
  318. STRPTR p1, p2, p3 ;
  319. /*
  320.  *   These are up to three arguments to be stuffed into the RexxMsg we
  321.  *   are building up, making the values available when the message is
  322.  *   finally replied to.  The values are stuffed into Args[2]..Args[4].
  323.  */
  324. {
  325.    struct RexxMsg *CreateRexxMsg() ;
  326.    STRPTR CreateArgstring() ;
  327.    register struct MsgPort *rexxport ;
  328.    register struct RexxMsg *RexxMsg ;
  329.  
  330. /*
  331.  *   If we have too many replies out there, we just return failure.
  332.  *   Note that you should check the return code to make sure your
  333.  *   message got out!  Then, we forbid, and make sure that:
  334.  *      - we have a rexx port open
  335.  *      - Rexx is out there
  336.  *      - the library is open
  337.  *      - we can create a message
  338.  *      - we can create an argstring
  339.  *
  340.  *   If all of these succeed, we stuff a few values and send the
  341.  *   message, permit, and return.
  342.  */
  343.    if (rexxPort == NULL || stillNeedReplies > MAXRXOUTSTANDING-1)
  344.       return(NULL) ;
  345.    RexxMsg = NULL ;
  346.    if (openRexxLib() && (RexxMsg =
  347.              CreateRexxMsg(rexxPort, extension, rexxPort->mp_Node.ln_Name)) &&
  348.              (RexxMsg->rm_Args[0] = CreateArgstring(s, (long)strlen(s)))) {
  349.       RexxMsg->rm_Action = RXCOMM ;
  350.       RexxMsg->rm_Args[1] = (STRPTR)f ;
  351.       RexxMsg->rm_Args[2] = p1 ;
  352.       RexxMsg->rm_Args[3] = p2 ;
  353.       RexxMsg->rm_Args[4] = p3 ;
  354.       RexxMsg->rm_Node.mn_Node.ln_Name = RXSDIR ;
  355.       Forbid() ;
  356.       if (rexxport = FindPort(RXSDIR))
  357.          PutMsg(rexxport, (struct Message *)RexxMsg) ;
  358.       Permit() ;
  359.       if (rexxport) {
  360.          stillNeedReplies++ ;
  361.          return(RexxMsg) ;
  362.       } else
  363.          DeleteArgstring(RexxMsg->rm_Args[0]) ;
  364.    }
  365.    if (RexxMsg)
  366.       DeleteRexxMsg(RexxMsg) ;
  367.    closeRexxLib() ;
  368.    return(NULL) ;
  369. }
  370. /*
  371.  *   This function is used to send out an ARexx message and return
  372.  *   immediately.  Its single parameter is the command to send.
  373.  */
  374. struct RexxMsg *asyncRexxCmd(s)
  375. char *s ;
  376. {
  377.    return(sendRexxCmd(s, NULL, NULL, NULL, NULL)) ;
  378. }
  379. /*
  380.  *   This function sets things up to reply to the message that caused
  381.  *   it when we get a reply to the message we are sending out here.
  382.  *   But first the function we pass in, which actually handles the reply.
  383.  *   Note how we get the message from the Args[2]; Args[0] is the command,
  384.  *   Args[1] is this function, and Args[2]..Args[4] are any parameters
  385.  *   passed to sendRexxCmd() as p1..p3.  We pass the result codes right
  386.  *   along.
  387.  */
  388. static void replytoit(msg)
  389. register struct RexxMsg *msg ;
  390. {
  391.    register struct RexxMsg *omsg ;
  392.  
  393.    omsg = (struct RexxMsg *)(msg->rm_Args[2]) ;
  394.    replyRexxCmd(omsg, msg->rm_Result1, msg->rm_Result2, NULL) ;
  395.    ReplyMsg((struct Message *)omsg) ;
  396. }
  397. /*
  398.  *   This function makes use of everything we've put together so far,
  399.  *   and functions as a synchronous Rexx call; as soon as the macro
  400.  *   invoked here returns, we reply to `msg', passing the return codes
  401.  *   back.
  402.  */
  403. struct RexxMsg *syncRexxCmd(s, msg)
  404. char *s ;
  405. struct RexxMsg *msg ;
  406. {
  407.    return(sendRexxCmd(s, (APTR)&replytoit, msg, NULL, NULL)) ;
  408. }
  409. /*
  410.  *   There are times when you want to pass back return codes or a
  411.  *   return string; call this function when you want to do that,
  412.  *   and return `1' from the user dispatch function so the main
  413.  *   event loop doesn't reply (because we reply here.)  This function
  414.  *   always returns 1.
  415.  */
  416. void replyRexxCmd(msg, primary, secondary, string)
  417. /*
  418.  *   The first parameter is the message we are replying to.
  419.  */
  420. register struct RexxMsg *msg ;
  421. /*
  422.  *   The next two parameters are the primary and secondary return
  423.  *   codes.
  424.  */
  425. register long primary, secondary ;
  426. /*
  427.  *   The final parameter is a return string.  This string is only
  428.  *   returned if the primary return code is 0, and a string was
  429.  *   requested.
  430.  *
  431.  *   We also note that we have replied to the message that came in.
  432.  */
  433. register char *string ;
  434. {
  435.    STRPTR CreateArgstring() ;
  436.  
  437. /*
  438.  *   Note how we make sure the Rexx Library is open before calling
  439.  *   CreateArgstring . . . and we close it down at the end, if possible.
  440.  */
  441.    if (primary == 0 && (msg->rm_Action & (1L << RXFB_RESULT))) {
  442.       if (string && openRexxLib())
  443.          secondary = (long)CreateArgstring(string, (long)strlen(string)) ;
  444.       else
  445.          secondary = 0L ;
  446.    }
  447.    msg->rm_Result1 = primary ;
  448.    msg->rm_Result2 = secondary ;
  449.    closeRexxLib() ;
  450. }
  451.